package org.testis.data.parser;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.DynamicMapParameter;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.testdriver.AtofEnumaration;
import org.testis.testdriver.TestPlanGlobalsHashMap;
import org.testis.testdriver.TestStepGlobalDataHashMap;
import org.testis.testdriver.translation.AtofValueTranslateService;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.webautomationmodule.ui.UIEnumaration.UIObjectParameter;


/**
 *
 * @author nurhat
 */
public class WAMDefaultValueVerificationDopDecorator extends WAMDopDecorator
{

    public WAMDefaultValueVerificationDopDecorator(
        WebAutomationModuleDataObject data,
        DynamicMapDesignTreeNode root)
    {
        super(data,
              root);
    }


    @Override
    protected boolean setBehavior()
    {
        String pageId = null;
        String abstract_link_id = null;
        if (getData().getDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_PAGE_ID)) {
            pageId = getData().getDataToApply().get(
                AtofModuleParameterNames.WEB_AUTO_PAGE_ID);
            if (pageId.equals("")) {
                pageId = null;
            }
        }
        if (getData().getDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_ABSTRACT_LINK_ID)) {
            abstract_link_id = getData().getDataToApply().get(
                AtofModuleParameterNames.WEB_AUTO_ABSTRACT_LINK_ID);
            if (abstract_link_id.equals("")) {
                abstract_link_id = null;
            }
        }
        if (pageId == null) {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "No  page id  parameter given for DefaultValueVerificationAction",
                getData().getMessageId());
            return false;
        }

        DynamicMapDesignTreeNode pageNode = findObject(pageId);
        if (pageNode == null) {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "No  page found(" + pageId +
                ") for DefaultValueVerificationAction",
                getData().getMessageId());
            return false;
        }
        DynamicMapDesignTreeNode[] childs = pageNode.getChildren();
        getData().getDataToApply().clear();
        getData().setActionType(AtofEnumaration.AtofWebAutomationModuleAction.GetAndVerify.
                                name());
        if (abstract_link_id != null) {
            getData().
                getDataToApply().put(abstract_link_id,
                                     "");
        }
        try {
            for (int i = 0; i < childs.length; i++) {
                String typeName = childs[i].getObject().getTypeName();
                DynamicMapParameter parValue =
                    childs[i].getObject().getParameterByName(
                    UIObjectParameter.Value.name());
                if (parValue != null) {
                    DynamicMapParameter parAbstractId =
                        childs[i].getObject().
                        getParameterByName(UIObjectParameter.AbstractId.name());
                    setDefaultsForObject(parAbstractId,
                                         childs[i]);

                }
            }
        } catch (Exception ex) {
            return false;
        }
        return true;
    }


    private void setDefaultsForObject(DynamicMapParameter parAbstractId,
                                      DynamicMapDesignTreeNode node) throws 
        Exception
    {

        if (parAbstractId == null) {
            LogSenderService.putMessageOnQueue("WebAutomationDataObjectParser",
                                               AtofLogLevel.ERROR,
                                               "No  abstract id parameter found for ( " +
                                               node.getObject().toString() +
                                               ") at DefaultValueVerificationAction",
                                               getData().getMessageId());
            return;
        }
        String abstractId = getParameterValue(parAbstractId);
        DynamicMapParameter parValue =
            node.getObject().getParameterByName(
            UIObjectParameter.Value.name());
        if (parValue != null) {
            String val = getParameterValue(parValue);
            String translatedVal;
            if (!val.equals("")) {
                try {
                    AtofValueTranslateService service = new AtofValueTranslateService(
                    TestPlanGlobalsHashMap.createInstance().getTestplanglobals(),
                    TestPlanGlobalsHashMap.createInstance().getTestplanlanguages(),
                    TestPlanGlobalsHashMap.createInstance().getTestplandutvalues(),
                    TestStepGlobalDataHashMap.createInstance().getTeststepglobals());

                    translatedVal = service.translateValue(
                        val);
                } catch (Exception ex) {
                    LogSenderService.putMessageOnQueue(
                        "WebAutomationDataObjectParser",
                        AtofLogLevel.ERROR,
                        val +
                        " not found in global or language variables. Error:" +
                        ex.getMessage(),
                        getData().getMessageId());
                    throw ex;
                }
                if (translatedVal != null) {
                    getData().
                        getDataToApply().put(abstractId,
                                             translatedVal);
                }
                else {
                    getData().
                        getDataToApply().put(abstractId,
                                             val);
                }
            }
        /*   else {
        getData().
        getDataToApply().put(abstractId,
        "");
        }*/
        }
        else {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "No  Value parameter found for ( " +
                abstractId + ") at DefaultValueVerificationAction",
                getData().getMessageId());
        }

    }
}